<script setup lang="ts" name="editColumns">
import { onMounted, nextTick } from 'vue'
import useEditColumns from '@/components/editData/hooks/editColumns'
import type { EditColumnsExport, MyColumns } from '@/types/loginType'
const {
  form,
  isActive,
  newImg,
  isEdit,
  columns,
  data,
  members,
  formSet,
  assigData,
  myColumns,
  columnClicked,
  loadImg,
  createColumn,
  getColumnFormData,
  searchKeyWords
} = useEditColumns()

onMounted(async () => {
  await myColumns()
  if (columns.value.length !== 0) {
    isEdit.value = false
    await nextTick(() => {
      columnClicked(columns.value[0].columnId)
    })
  }
})
defineExpose<EditColumnsExport>({ getColumnFormData })
const emit = defineEmits(['changed'])
const formChanged = async (e: HTMLInputElement) => {
  // 修改 data 值
  formSet.value.add(e)
  assigData(e.name as keyof MyColumns, e.value)
  await nextTick()
  emit('changed')
}
</script>

<template>
  <div id="editColumns">
    <ul id="menu">
      <li class="active">已创建的专栏</li>
      <li
        class=""
        :class="{ active: isActive === item.columnId }"
        v-for="item in columns"
        :key="item.columnId"
        @click="columnClicked(item.columnId)"
      >
        {{ item.columnName }}
      </li>
      <li class="default-button" :class="{ active: isActive === '' }" @click="createColumn">
        <Plus class="add" />
      </li>
    </ul>
    <div id="content">
      <form class="top" ref="form" @change="formChanged($event.target as HTMLInputElement)">
        <span>
          <label for="img">
            <img :src="newImg" alt="" v-if="isEdit" />
            <img :src="data?.columnImg || '/fontImg/Col.png'" alt="" v-else />
            <p>{{ data?.columnId }}</p>
            <input
              type="file"
              name="columnImg"
              id="img"
              accept="image/*"
              v-show="false"
              @change="loadImg($event.target as HTMLInputElement)"
            />
          </label>
          <input type="text" name="columnId" id="id" v-show="false" :value="data?.columnId" />
        </span>
        <span>
          <label for="name">专栏名</label>
          <input type="text" name="columnName" id="name" :value="data?.columnName" required />
        </span>
        <span>
          <label for="msg">简述信息</label>
          <textarea name="columnMsg" id="msg" :value="data?.columnMsg"></textarea>
        </span>
      </form>
      <div class="bottom">
        <span>成员列表</span>
        <ul>
          <li v-for="user in members" :key="user.id" @click="searchKeyWords(user.name, 2)">
            <img :src="user.img" alt="" />
            <p>{{ user.name }}</p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.add {
  width: 20px;
  height: 20px;
}

#editColumns {
  margin-top: 10px;
  display: grid;
  grid-template-columns: 120px 1fr;
}
#menu {
  display: flex;
  flex-direction: column;
  gap: 6px;
  list-style: none;
  > .active {
    background-color: var(--color2);
    color: var(--color3);
    border: none;
  }
  > li:hover:not(.active) {
    background-color: var(--color2);
    color: var(--color3);
  }
  > li {
    cursor: pointer;
    padding: 10px;
    border-radius: var(--radius) 0 0 var(--radius);
    border: 1px dashed var(--color2);
    border-right: none;
    color: var(--color3);
  }
}
#content {
  border: 1px solid var(--color2);
  border-radius: 0 var(--radius) var(--radius) 0;
  .top {
    margin: 20px 0;
    display: flex;
    align-items: center;
    justify-content: space-around;
    > span:nth-child(1) {
      height: 126px;
      width: 126px;
      > label > img {
        cursor: pointer;
        display: block;
        margin: 0 auto;
        width: 96px;
        height: 96px;
        object-fit: cover;
        border-radius: var(--radius);
      }
      > label > p {
        margin: 6px 0;
        text-align: center;
        color: var(--color3);
      }
    }
    > span:nth-child(2),
    > span:nth-child(3) {
      > label {
        display: block;
        height: 40px;
        line-height: 40px;
        text-align: center;
        background-color: var(--color2);
        color: var(--color3);
        border-radius: var(--radius) var(--radius) 0 0;
      }
      > input,
      > textarea {
        font-size: 16px;
        color: var(--color-font);
        width: 100%;
        border: none;
        padding: 6px;
        box-sizing: border-box;
        resize: none;
        border-radius: 0 0 var(--radius) var(--radius);
      }
      > input {
        height: 40px;
        text-align: center;
      }
      > textarea {
        height: 64px;
      }
    }
  }
  .bottom {
    border-top: 1px solid var(--color2);
    min-height: 600px;
    > span {
      display: block;
      text-align: center;
      padding-top: 6px;
      color: var(--color3);
    }

    > ul {
      margin: 10px;
      display: grid;
      gap: 10px;
      grid-template-columns: repeat(auto-fill, minmax(84px, 1fr));
      list-style: none;
      > li:hover {
        outline: 2px solid var(--color3);
        color: var(--color3);
      }
      > li {
        cursor: pointer;
        padding: 6px;
        border-radius: var(--radius);
        padding: 10px;
        background-color: var(--color2);
        > img {
          display: block;
          margin: 0 auto;
          width: 64px;
          height: 64px;
          object-fit: cover;
          border-radius: var(--radius);
        }
        > p {
          margin-top: 6px;
          text-align: center;
        }
      }
    }
  }
}
</style>
